package com.teamwork.ZuccEcho.Repository;

import com.teamwork.ZuccEcho.Entity.Qnquestion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface QnQuestionApiRepository extends JpaRepository<Qnquestion, Long>, JpaSpecificationExecutor<Qnquestion> {
    Qnquestion findById(long id);

    @Query(value="select MAX(qnq_id) from qnquestion", nativeQuery = true)
    Long findPublicedQnqId();

    @Query(value="select qno_id from qnoptions where qnq_id = ?1", nativeQuery = true)
    List<Long> searchAllQnOptionsByQuestionId(long qnq_id);

    @Query(value="select * from qnquestion where qnq_id = ?1", nativeQuery = true)
    List<Qnquestion> searchByQnQuestionId(long qnq_id);

    @Transactional
    @Modifying
    @Query(value="DELETE from qnoptions where qno_id= ?1", nativeQuery = true)
    void deleteOptionByOId(long option_Id);

    @Transactional
    @Modifying
    @Query(value="UPDATE qnquestion SET qn_id = ?1 , qnq_type = ?2 , qnq_name = ?3 , " +
            "qnq_isrequired = ?4 , qnq_content = ?5 " +
            "WHERE qnq_id = ?6", nativeQuery = true)
    void modifyQnQuestionByQnqId(long qn_id,int qnq_type,String qnq_name,
                                 boolean qnq_isrequired,String qnq_content,long qnq_id);
}
